package com.jlastu.znb.presenter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import androidx.annotation.NonNull;

import com.jlastu.znb.base.WeakRefHandler;
import com.jlastu.znb.contract.DetailContract;
import com.jlastu.znb.entity.Goods;
import com.jlastu.znb.entity.Photo;

import java.util.ArrayList;
import java.util.List;

public class DetailPresenter implements DetailContract.IProductDetailPresenter {
    private static final int WHAT_GET_GOODS_LIST = 0;
    private DetailContract.IProductDetailView mProductDetailView;
    private Context mContext;

    public DetailPresenter(DetailContract.IProductDetailView ProductDetailView, Context context) {
        mProductDetailView = ProductDetailView;
        mContext = context;
    }

    //根据goodsid查询商品数据
    @Override
    public void getGoodsIdList(int goodsid) {
        List<Goods> list = new ArrayList<>();
        new Thread(new Runnable() {
            @SuppressLint("Range")
            @Override
            public void run() {
                Uri uri = Uri.parse("content://com.jlastu.znb.provider/goods/" + goodsid);
                try (Cursor cursor = mContext.getContentResolver().query(
                        uri, null, null, null)) {
                    if (cursor != null && cursor.moveToFirst()) {
                        do {
                            Goods goods = new Goods();
                            int goods_id = cursor.getInt(cursor.getColumnIndex("_id"));
                            goods.setId(goods_id);
                            goods.setName(cursor.getString(cursor.getColumnIndex("goods_name")));
                            goods.setPrice(cursor.getDouble(cursor.getColumnIndex("goods_price")));
                            goods.setUnit(cursor.getString(cursor.getColumnIndex("goods_unit")));
                            //goods.setBrowseCount(cursor.getInt(cursor.getColumnIndex("goods_browse")));
                            goods.setMessage(cursor.getString(cursor.getColumnIndex("goods_message")));
                            goods.setAddress(cursor.getString(cursor.getColumnIndex("goods_address")));
                            //goods.setGoods_userid(cursor.getInt(cursor.getColumnIndex("goods_userid")));
                            //int user_id = cursor.getInt(cursor.getColumnIndex("goods_userid"));

                            Uri photoUri = Uri.parse("content://com.jlastu.znb.provider/photo");
                            Cursor photoCursor = mContext.getContentResolver().query(
                                    photoUri,
                                    null,
                                    "goods_id = ?",
                                    new String[]{String.valueOf(goodsid)},
                                    null);

                            if (photoCursor != null && photoCursor.moveToFirst()) {
                                do {
                                    Photo photo = new Photo();
                                    photo.setId(photoCursor.getInt(photoCursor.getColumnIndex("_id")));
                                    photo.setData(photoCursor.getBlob(photoCursor.getColumnIndex("image")));
                                    goods.addPhoto(photo);
                                } while (photoCursor.moveToNext());
                            }
                            /*//查用户名,头像,电话
                            Uri useruri = Uri.parse("content://com.example.group5th.provider/user/" + user_id);
                            Cursor userCursor = mContext.getContentResolver().query(
                                    useruri, null, null, null);
                            if (userCursor != null && userCursor.moveToFirst()) {
                                User user = new User();
                                user.setUserId(user_id);
                                user.setUserName(userCursor.getString(userCursor.getColumnIndex("user_name")));
                                user.setData(userCursor.getBlob(userCursor.getColumnIndex("user_touxiang")));
                                user.setUserPhone(userCursor.getInt(userCursor.getColumnIndex("user_phone")));
                                goods.addUser(user);
                            }*/

                            list.add(goods);
                        } while (cursor.moveToNext());
                    }
                }

                //子线程通过Handler发送消息，完成线程之间的通信。
                Message message = Message.obtain();
                message.obj = list;
                message.what = WHAT_GET_GOODS_LIST;
                mHandler.sendMessage(message);
            }
        }).start();
    }

    Handler mHandler = new WeakRefHandler<>(Looper.getMainLooper(), this, new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            int what = msg.what;
            switch (what) {
                case WHAT_GET_GOODS_LIST:
                    mProductDetailView.showGoodsList((List<Goods>) msg.obj);
                    break;
            }
            return false;
        }
    });
}
